package cn.qg.search.controller;

import cn.qg.beans.dto.Dto;
import cn.qg.beans.pojo.QgGoods;
import cn.qg.beans.pojo.QgGoodsTempStock;
import cn.qg.beans.pojo.QgUser;
import cn.qg.common.DtoUtil;
import cn.qg.common.EmptyUtils;
import cn.qg.common.ErrorCode;
import cn.qg.mapper.qggoodstempstock.QgGoodsTempStockMapper;
import cn.qg.search.service.excetion.LoginFailedException;
import cn.qg.search.service.goods.GoodsService;

import cn.qg.search.service.qgGoodsTempStock.QgGoodsTempStockService;
import cn.qg.search.service.token.TokenServiceImpl;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 12:14
 */
@RestController
public class GoodsController {
    @Resource
    TokenServiceImpl tokenServiceImpl;
    @Resource
    QgGoodsTempStockService qgGoodsTempStockService;
    @Resource
    GoodsService goodsService;
    @RequestMapping(value = "/api/queryGoodsById",method = RequestMethod.POST,produces = {"application/json"})
    public Dto queryGoodsById(@RequestParam String id){


        try {
            QgGoods qgGoods= goodsService.getGoodsById(id);
            System.out.println(qgGoods.toString());
            if(EmptyUtils.isEmpty(qgGoods)){

                throw new LoginFailedException("数据不存在！");
            }

            return DtoUtil.returnDataSuccess(qgGoods);

        }catch (LoginFailedException e){
            e.getMessage();
            return DtoUtil.returnFail(e.getMessage(), ErrorCode.AUTH_AUTHENTICATION_FAILED);


        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("数据查找异常！",ErrorCode.AUTH_UNKNOWN);
        }

    }


@RequestMapping(value = "api/sendQgGoodsMessage",method = RequestMethod.POST,produces = {"application/json"})
   public Dto sendQgGoodsMessage(@RequestParam String goodsId,@RequestParam String token){

        try {
            //判断token 是否过期
            QgUser qgUser = tokenServiceImpl.getItripUserByToken(token);
            if(qgUser==null){
                return DtoUtil.returnFail("token已经过期","0001");
            }
            //判断是否有库存
            QgGoods qgGoods= goodsService.getGoodsById(goodsId);
            if(qgGoods.getStock()<=0){
                return DtoUtil.returnFail("抢购失败","0002");
            }
            //判断是否已经抢购过
            Integer  num= qgGoodsTempStockService.getGoodsTempStockById(goodsId,qgUser.getId());
            if( num>0){
                return DtoUtil.returnFail("用户已抢购，请勿重复抢购","3001");
            }
            //修改库存
            qgGoods.setStock(qgGoods.getStock()-1);
           Integer num1= goodsService.upgoods(qgGoods);
            //存入临时库存表
            QgGoodsTempStock qgGoodsTempStock = new QgGoodsTempStock();
            qgGoodsTempStock.setId(Integer.toString(qgGoods.getStock()));
            qgGoodsTempStock.setGoodsId(goodsId);
            qgGoodsTempStock.setStatus(0);
            qgGoodsTempStock.setUserId(qgUser.getId());
            qgGoodsTempStock.setCreatedTime(new Date());
            qgGoodsTempStock.setUpdatedTime(new Date());
            qgGoodsTempStockService.addQgGoodsTempStock(qgGoodsTempStock);
            return DtoUtil.returnSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail(e.getMessage(),"3003");
        }

}
}
